(function($, im, doc) {
	function moveEnd(obj) {
		obj.selectionStart = obj.selectionEnd = len;
	}

	var selectFaceTap = function(event) {
		//im.btnMsgImage.setAttribute("for", im.boxMsgText.innerHTML == '' ? '' : 'msg-text');
		var faceImg = this.getAttribute('src');
		im.boxMsgText.innerHTML += '<img src="' + faceImg + '" width="24" height="24"/>';
		//im.boxMsgText.innerHTML.replace(new RegExp('\n', 'gm'), '\n-');
		doc.querySelector('#msg-showtxt').style.display = 'block';
		doc.querySelector('#msg-showmore').style.display = 'none';
		//event.detail.gesture.preventDefault();
		/*
		if(im.boxMsgText.innerHTML != '') {
			doc.querySelector('.mui-icon-paperplane').style.display = 'block';
			doc.querySelector('.mui-icon-plus').style.display = 'none';
		} else {
			doc.querySelector('.mui-icon-paperplane').style.display = 'none';
			doc.querySelector('.mui-icon-plus').style.display = 'block';
		}*/
		//im.boxMsgText.focus();
	};
	var msgTextFocus = function() {
		im.boxMsgText.focus();

		setTimeout(function() {
			im.boxMsgText.focus();
		}, 150);

	}
	var footLeftTouch = function(event) {
		//console.log($.os.ios);

		//if(doc.querySelector("#msg-more").classList.contains('paperplane')) {
		//msgTextFocus();
		//	im.showKeyboard();
		event.preventDefault();
		//}
	};
	//发送文字
	var sendTextTap = function(event) {
		//console.log(im.footerPadding);

		//	event.detail.gesture.preventDefault();
		if(im.boxMsgText.innerHTML != '') {
			im.sendMsg({
				sender: 'self',
				type: 'text',
				content: im.boxMsgText.innerHTML.replace(new RegExp('\n', 'gm'), '<br/>'),
				time: app.dateStr(Date.parse(new Date()) / 1000)
			});
			im.boxMsgText.innerHTML = '';

			//im.boxMsgText.focus();

			setTimeout(function() {
				//im.boxMsgText.focus();
				doc.querySelector('#Gallery').style.display = 'none';
				doc.querySelector('#biaoqingbao').style.display = 'none';
				doc.querySelector('footer').style.bottom = '0px';
				im.content.style.paddingBottom = 10;
			}, 80);
			$.trigger(im.boxMsgText, 'input', null);
			event.detail.gesture.preventDefault();
		} else {
			$.toast("请输入信息");
		}
	};
	//显示发送按钮
	var showboxMsgTextSend = function(event) {
		//console.log(im.boxMsgText.innerHTML);
		//						ui.btnMsgImage.classList[ui.boxMsgText.innerHTML == '' ? 'remove' : 'add']('mui-icon-paperplane');
		if(im.boxMsgText.innerHTML != '') {
			doc.querySelector('#msg-showtxt').style.display = 'block';
			doc.querySelector('#msg-showmore').style.display = 'none';
		} else {
			doc.querySelector('#msg-showtxt').style.display = 'none';
			doc.querySelector('#msg-showmore').style.display = 'block';
		}
		//console.log(im.footer.style.height);
		//im.btnMsgImage.setAttribute("for", im.boxMsgText.innerHTML == '' ? '' : 'msg-text');
		//im.h.innerText = im.boxMsgText.innerHTML.replace(new RegExp('\n', 'gm'), '\n-') || '-';
		//im.footer.style.height = (im.h.offsetHeight + im.footerPadding) + 'px';
		
		im.content.style.paddingBottom = im.footer.style.height;
		//console.log(im.footerPadding);
	};
	var resizeMsgFooter = function() {
		if($.os.ios) {
			var originalHeight = document.documentElement.clientHeight || document.body.clientHeight;
			doc.querySelector('footer').style.top = originalHeight - 50 + 'px';
			doc.querySelector('footer').style.position = 'static';
		}
	};
	var focus = false;
	var boxMsgTextTap = function(event) {

		//console.log(doc.querySelector('footer').style.bottom);
		doc.querySelector('#biaoqingbao').style.display = 'none';
		doc.querySelector('#Gallery').style.display = 'none';
		
		doc.querySelector('footer').style.bottom = '0px';
		if($.os.ios) {
			//doc.querySelector('footer').style.top = '300px';
			//doc.querySelector('footer').style.zIndex = 99999;
			// doc.querySelector('footer').style.position = 'fixed';
		}
		//im.content.style.paddingBottom = im.footer.style.height;
		//doc.querySelector('#biaoqing').classList.add('fa-smile-o');
		//doc.querySelector('#biaoqing').classList.remove('fa-keyboard-o');
		//im.content.style.paddingBottom = '60px';
		im.showKeyboard();
		//im.boxMsgText.focus();
		/*
		im.boxMsgText.focus();
		setTimeout(function() {
			im.boxMsgText.focus();
		}, 0);
		focus = true;
		setTimeout(function() {
			focus = false;
		}, 50);*/
		//event.detail.gesture.preventDefault();
	};
	var closeFootBox = function() {
		if(!focus) {
			//im.boxMsgText.blur();
		}
		if($.os.ios) {
			// doc.querySelector('footer').style.position = 'fixed';
		}
		//console.log('click');
		doc.querySelector('#biaoqingbao').style.display = 'none';
		doc.querySelector('#Gallery').style.display = 'none';
		doc.querySelector('footer').style.bottom = '0px';

		//doc.querySelector('#biaoqing').classList.add('fa-smile-o');
		//doc.querySelector('#biaoqing').classList.remove('fa-keyboard-o');
		doc.querySelector('#msg-showtxt').style.display = 'none';
		doc.querySelector('#msg-showmore').style.display = 'block';
		im.boxMsgText.blur();
		im.content.style.paddingBottom = doc.querySelector('footer').offsetHeight + 10 + 'px';
	};
	
	
	
	function showMoreTap() {
		
		if(doc.querySelector('#Gallery').style.display !== 'block') {
			im.boxMsgText.blur();
			setTimeout(openMoreTap, 20);
		} else {
			closeMoreTap();
		}
	};
	
	function openMoreTap() {
		
		doc.querySelector('#biaoqingbao').style.display = 'none';
		doc.querySelector('#Gallery').style.display = 'block';
		doc.querySelector('footer').style.bottom = doc.querySelector('#Gallery').offsetHeight + 'px';
		//console.log(doc.querySelector('#Gallery').offsetHeight);
	}
	
	function closeMoreTap() {
		
		doc.querySelector('footer').style.bottom = '0px';
		doc.querySelector('#Gallery').style.display = 'none';
		doc.querySelector('#biaoqingbao').style.display = 'none';
	}
	
	
	
	function showBiaoqing(event) {
		
		im.boxMsgText.style.display = 'block';
		im.boxMsgSound.style.display = 'none';
		doc.getElementById('msg-image').setAttribute('class', "fa fa-wpexplorer");
		
		if (doc.querySelector('#biaoqingbao').style.display !== 'block') {
			im.boxMsgText.blur();
			setTimeout(openBiaoQing, 20);
		} else {
			closeBiaoQing();
		}
	};
		
	function openBiaoQing() {
		doc.querySelector('#biaoqingbao').style.display = 'block';
		doc.querySelector('#Gallery').style.display = 'none';
		doc.querySelector('footer').style.bottom = doc.querySelector('#biaoqingbao').offsetHeight + 'px';
	}
	
	function closeBiaoQing() {
		doc.querySelector('#biaoqingbao').style.display = 'none';
		doc.querySelector('#Gallery').style.display = 'none';
		doc.querySelector('footer').style.bottom = '0px';
	}
	
	
	
	$.plusReady(function() {
		doc.querySelector('#biaoqingbao').innerHTML = template('face-templete', {});
		$("#biaoqingbao").slider({
			interval: 0
		});
		//选择表情
		$('#selectFace').on('tap', 'img', selectFaceTap);
		//console.log(JSON.stringify(im));
		//解决长按“发送”按钮，导致键盘关闭的问题；
		//im.footerLeft.addEventListener('touchstart', footLeftTouch);
		//im.footerLeft.addEventListener('touchmove', footLeftTouch);
		//发送文字
		var paper = doc.querySelector('.paperplane');
		if(paper) {
			paper.addEventListener('tap', sendTextTap);
		}
		//显示发送按钮
		im.boxMsgText.addEventListener('input', showboxMsgTextSend);
		//显示键盘 
		im.boxMsgText.addEventListener('click', boxMsgTextTap, false);

		//点击消息列表，关闭键盘
		//im.areaMsgList.addEventListener('click', closeFootBox, false);
		//im.areaMsgList.addEventListener('resize', resizeMsgFooter, false);
		//显示更多
		doc.querySelector('#msg-showmore').addEventListener('tap', showMoreTap);
		//显示表情
		doc.querySelector('#biaoqing').addEventListener('tap', showBiaoqing, false);
	});
}(mui, im, document));